#include <arch/riscv64/riscv64.h>
#include <mm/mm.h>
#include <drivers/fdt/fdt_context.h>

extern uint64_t cpuid_to_hartid[MAX_CPU_NUM];
void arch_early_init()
{
    
    trap_init();

//     uint64_t tmp = (uint64_t)alloc_frames_bytes(STACK_SIZE);
// printk("alloc stack base=%lx\n", tmp);

// csr_write(sscratch, tmp + STACK_SIZE);

// uint64_t readback;
// asm volatile("csrr %0, sscratch" : "=r"(readback));
// printk("sscratch written=%lx readback=%lx\n", tmp + STACK_SIZE, readback);


    csr_write(sscratch, (uint64_t)alloc_frames_bytes(STACK_SIZE) + STACK_SIZE);
    // fdt_init();
    // smp_init();
    asm volatile("mv gp, %0" ::"r"(cpuid_to_hartid[current_cpu_id]));

    timer_init_hart(cpuid_to_hartid[current_cpu_id]);

}
void arch_init()
{
    syscall_init();
    // arch_enable_interrupt();
}